from django.contrib.auth.models import User
from django.db import models

from utils.ModelMixin import DataTimeModelMixin,ModelSetMixin


# Create your models here.

class Label(DataTimeModelMixin):
    name = models.CharField(max_length=20,verbose_name='标签名',unique=True)

    def __str__(self):
        return self.name

class Article(ModelSetMixin):
    STATUS_CHOICES = (
        (0,'未发布'),
        (1,'发布'),
    )
    title = models.CharField(max_length=100,verbose_name='标题')
    digest = models.CharField(max_length=300,verbose_name='摘要')
    content = models.TextField(verbose_name='内容')
    page_view = models.IntegerField(verbose_name='浏览量',default=0)
    priority = models.IntegerField(verbose_name='优先级',default=0)
    status = models.IntegerField(verbose_name='状态',default=0,choices=STATUS_CHOICES)
    label = models.ForeignKey(Label,on_delete=models.CASCADE)
    user = models.ForeignKey(User,on_delete=models.CASCADE)

    class Meta:
        ordering = ['-priority','-page_view','-create_time']
        verbose_name = '文章'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s-%s'%(self.title,self.user.username)

class Comment(ModelSetMixin):
    content = models.TextField(verbose_name='评论内容')
    article = models.ForeignKey(Article,on_delete=models.CASCADE)
    user = models.ForeignKey(User,on_delete=models.CASCADE)
    parent_comment = models.ForeignKey('self',verbose_name='父级评论',on_delete=models.CASCADE,related_name='parent_comment_set',null=True,blank=True,)
    reply_comment = models.ForeignKey('self',verbose_name='回复评论',on_delete=models.CASCADE,related_name='reply_comment_set',null=True,blank=True,)

    class Meta:
        ordering = ['-create_time']

    def __str__(self):
        return '%s-%s'%(self.user.username,self.content)